package com.etc.shiro.testcontroller;

import com.etc.common.CodeMessage;
import com.etc.jwt.JwtUtil;
import com.etc.shiro.PasswordHelper;
import com.etc.shiro.model.RoleMst;
import com.etc.shiro.model.UserMst;
import com.etc.shiro.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping
public class HomeController {
    @Autowired
    private UserService userService;
    @Autowired
    private PasswordHelper passwordHelper;

    @GetMapping("login")
    public Object login() {
        return "Here is Login page";
    }

    @GetMapping("unauthc")
    public Object unauthc() {
        return "Here is Unauthc page";
    }


    @PostMapping("doLogin")
    public CodeMessage login(@RequestBody HashMap<String, String> map) {

        UsernamePasswordToken token = new UsernamePasswordToken(map.get("username"), map.get("password"));
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);
        } catch (IncorrectCredentialsException ice) {
            return CodeMessage.message("password error!", null);
        } catch (UnknownAccountException uae) {
            return CodeMessage.message("uername error!", null);
        }

        UserMst user = userService.findUserByName(map.get("username"));
        String jwttoken = JwtUtil.sign(user.getUname(), user.getPassword());
        subject.getSession().setAttribute("user", user);
        CodeMessage cm = new CodeMessage();
        cm.setCode("20000");
        cm.setMessage("success");
        cm.setToken(jwttoken);

        return cm;
    }

    @GetMapping("register")
    public Object register(@RequestParam String username, @RequestParam String password) {
        UserMst user = new UserMst();
        user.setUname(username);
        user.setPassword(password);
        passwordHelper.encryptPassword(user);

        userService.saveUser(user);
        return "SUCCESS";
    }

    @GetMapping("info")
    public CodeMessage info(String token) {

        String username = JwtUtil.getUsername(token);
        UserMst user = userService.findUserByName(username);
        String jwttoken = JwtUtil.sign(user.getUname(), user.getPassword());
        CodeMessage cm = new CodeMessage();
        cm.setCode("20000");
        cm.setMessage("success");
        cm.setToken(jwttoken);

        List<String> rname = user.getRlist().stream().map(RoleMst::getRname).collect(Collectors.toList());
        HashMap<String, Object> hm = new HashMap<>();
        hm.put("roles", rname);
        hm.put("name", user.getUname());
        hm.put("avatar", user.getUname());
        hm.put("introduction", user.getUname());

        cm.setData(hm);
        return cm;
    }

    @RequiresRoles("admin")
    @GetMapping("/authc/admin")
    public String aaa() {
         return "SSUCCESSS";
    }

    @RequiresRoles("employ")
    @GetMapping("/authc/bbb")
    public String bbb() {
        return "SSUCCESSSbb";
    }
}
